Exercise 5: The Classic FizzBuzz!

Directions

Write a program that console logs the numbers from 1 to n. But for multiples of three print “fizz” instead of the number and for the multiples of five print “buzz”. For numbers which are multiples of both three and five print “fizzbuzz”.

Example

fizzBuzz(5);
  1
  2
  fizz
  4
  buzz

Guidelines

  • Trick: use Modulo operator
  • Try not to use fancy code

Solution

  • line 3: i % 15 === 0 also ok
In [2]:
function fizzBuzz(n) {
  for (let i=1; i<=n; i++){
    if (i % 3 === 0 && i % 5 === 0){
      console.log("fizzbuzz");
    }
    else if (i % 3 === 0){
      console.log("fizz");
    } else if (i % 5 === 0) {
      console.log("buzz");
    } else
      console.log(i);
    }
}
In [3]:
fizzBuzz(5);
1
2
fizz
4
buzz